import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse, HttpHeaders } from '@angular/common/http';
import { Event } from './alarm.model';
import { ConfigService } from 'app/config/config.service';

const SEVERITIES = ['high11', 'moderate11', 'low11'];

const TYPES = ['accident', 'diversion', 'heavy traffic', 'obstacle', 'misc'];

const SUBTYPES = ['heavy traffic', 'police activity', 'oil spill', '-'];

const START_TIMES = ['25/07/2018 08:00', '25/07/2018 11:00', '25/07/2018 15:30', '25/07/2018 15:40'];

const END_TIMES = ['25/07/2018 08:30', '25/07/2018 09:00', '25/07/2018 15:00', '25/07/2018 16:50', '25/07/2018 18:20', '-'];

const ROAD_IDS = ['E 11', 'E 68', 'D 71', 'D 85', 'D 92'];

const ROAD_NAMES = ['Sheikh Zayed Road', 'Dubai-Al Ain Road', 'Al Safa Street', 'Al Mina Road', 'Baniyas Road'];

const UPDATE_TIMES = [
    '25/07/2018 09:00',
    '25/07/2018 09:05',
    '25/07/2018 11:02',
    '25/07/2018 11:05',
    '25/07/2018 12:05',
    '25/07/2018 15:05',
    '25/07/2018 15:31',
    '25/07/2018 15:35',
    '25/07/2018 15:45'
];
const ZONE = ['Zone1', 'Zone2', 'Zone3', 'Zone4', 'Zone5', 'Zone6', 'Zone7', 'Zone8', 'Zone9', 'Zone10', 'Zone11'];
const MASKOUT = ['Yes', 'No'];
const SOURCE = ['CCTV', 'SCOOT', 'RWIS', 'BT'];
const ALARMTYPE = ['CONSTANT', 'INTERMITTENT'];
const LOCK_STATUSES = ['yes', 'no'];
const STATUSES = ['notified', 'confirmed', 'over', 'in progress'];
const DURATIME = ['1h30m25s', '2h30m', '56m', '30m5s', '45m18s', '20m38s'];
const LANE_BLOCKAGES = ['arrow-up.png', 'cross.png'];
const ALARMSTATUS = ['OPEN', 'TRUE', 'UNVERIFIED'];
const DESCRIPTION = ['Occupancy Too High', 'Presence Incident'];

@Injectable({
    providedIn: 'root'
})
export class AlarmService {
    resourceUrl = this.config.apiUrl.alert;
    public events: Event[] = [];
    private apiUrl_user_online = this.config.apiUrl.user;
    private apiUrl_incident_online = this.config.apiUrl.incident;
    constructor(private http: HttpClient, private config: ConfigService) {}
    public description() {
        return [
            { name: 'congestion', value: 'congestion' },
            { name: 'vehicle non-working', value: 'vehicle non-working' },
            { name: 'vehicle converse running', value: 'vehicle converse running' },
            { name: 'Spilled material, pedestrian', value: 'Spilled material, pedestrian' },
            { name: 'vehicle slow-moving', value: 'vehicle slow-moving' },
            { name: 'smog', value: 'smog' }
        ];
    }
    public renderHeader() {
        return [
            {
                name: 'Detection Date',
                key: null,
                value: 'detectionTime',
                isChecked: true
            },
            {
                name: 'Alert ID',
                key: null,
                value: 'alertID',
                isChecked: true
            },
            {
                name: 'Zone ID',
                key: null,
                value: 'zoneID',
                isChecked: true
            },
            {
                name: 'Road Name',
                key: null,
                value: 'roadName',
                isChecked: true
            },
            {
                name: 'Description',
                key: null,
                value: 'description',
                isChecked: true
            },
            {
                name: 'Alert Source',
                key: null,
                value: 'alertSource',
                isChecked: true
            },
            {
                name: 'Ack By',
                key: null,
                value: 'ackBy',
                isChecked: true
            },
            {
                name: 'Ack Date',
                key: null,
                value: 'ackTime',
                isChecked: true
            },
            {
                name: 'Ack Value',
                key: null,
                value: 'ackValue',
                isChecked: true
            }
        ];
    }

    public generateEvents(howMany: number) {
        this.events = [];
        for (let i = 1; i <= howMany; ++i) {
            const numLaneBlockages = Math.floor(Math.random() * 6) + 1;
            const laneBlockagesArr = [];
            for (let j = 1; j <= numLaneBlockages; ++j) {
                laneBlockagesArr.push(LANE_BLOCKAGES[Math.floor(Math.random() * LANE_BLOCKAGES.length)]);
            }

            this.events.push(
                new Event({
                    id: 'E' + (Math.floor(Math.random() * 50000) + 10000),
                    severity: SEVERITIES[Math.floor(Math.random() * (SEVERITIES.length - 1)) + 1],
                    type: TYPES[Math.floor(Math.random() * TYPES.length)],
                    subType: SUBTYPES[Math.floor(Math.random() * SUBTYPES.length)],
                    startTime: START_TIMES[Math.floor(Math.random() * START_TIMES.length)],
                    endTime: END_TIMES[Math.floor(Math.random() * END_TIMES.length)],
                    roadId: ROAD_IDS[Math.floor(Math.random() * ROAD_IDS.length)],
                    roadName: ROAD_NAMES[Math.floor(Math.random() * ROAD_NAMES.length)],
                    updateTime: UPDATE_TIMES[Math.floor(Math.random() * UPDATE_TIMES.length)],
                    status: ALARMSTATUS[Math.floor(Math.random() * ALARMSTATUS.length)],
                    laneBlockages: laneBlockagesArr,
                    duratime: DURATIME[Math.floor(Math.random() * DURATIME.length)],
                    zone: ZONE[Math.floor(Math.random() * ZONE.length)],
                    source: SOURCE[Math.floor(Math.random() * SOURCE.length)],
                    alarmType: ALARMTYPE[Math.floor(Math.random() * ALARMTYPE.length)],
                    description: DESCRIPTION[Math.floor(Math.random() * DESCRIPTION.length)]
                })
            );
        }
        return this.events;
    }

    getTrafficAlert(data) {
        return this.http.post<any>(this.resourceUrl + '/vnqnalertmgt/trafficAlert/get', data);
    }
    ackTrafficAlert(data, header) {
        let headers = new HttpHeaders();
        // headers = headers.append('Authorization', this.ls.getAccessToken());
        headers = headers.append('isForce', header.isFource);
        headers = headers.append('zoneId', header.zoneId);
        return this.http.get<any>(
            this.resourceUrl +
                '/vnqnalertmgt/trafficAlert/ack?alertId=' +
                data.alertId +
                '&ackValue=' +
                data.ackValue +
                '&userName=' +
                data.userName +
                '&zoneId=' +
                data.zoneId,
            { headers }
        );
    }
    // 获取用户列表
    getAllUser(param) {
        // const url = `${this.apiUrl_equip_online}/userMgt/user/getAllUser`;
        return this.http.post<any>(this.apiUrl_user_online + '/userMgt/user/getAllUserInfoByPage', param);
    }
    getHistoricalAlert(data) {
        return this.http.post<any>(this.resourceUrl + '/vnqnalertmgt/trafficAlert/getHist', data);
    }
    // data
    formAlertData(array, date, ackDate, pageParam, zoneList) {
        const data = {
            condition: {
                ackBy: null,
                ackTime: {
                    startTime: null,
                    endTime: null
                },
                ackValue: null,
                alertID: null,
                alertSource: null,
                detectionTime: {
                    endTime: null,
                    startTime: null
                },
                roadName: null,
                zoneID: null,
                description: null,
                zoneList
            },
            pageNum: pageParam.pageNo,
            pageSize: pageParam.pageSize
        };
        array.forEach((item, i) => {
            // tslint:disable-next-line:prefer-const
            let { key, value } = item;
            if (key === '') {
                key = null;
            }
            if (value !== 'ackTime' && value !== 'detectionTime') {
                data.condition[value] = key;
            }
        });
        data.condition.detectionTime.startTime = date.startTime;
        data.condition.detectionTime.endTime = date.endTime;
        data.condition.ackTime.startTime = ackDate.startTime;
        data.condition.ackTime.endTime = ackDate.endTime;
        return data;
    }
    createIr(data, header: any) {
        let headers = new HttpHeaders();
        headers = headers.append('isForce', header.isFource);
        headers = headers.append('zoneId', header.zoneId);
        return this.http.post<any>(this.apiUrl_incident_online + '/vnqnincidentmgt/createIR', data, { headers });
    }
}
